﻿namespace Refract.Event
{
    using System;

    using Refract.Composition;

    /// <summary>
    /// A subscription to a query.  
    /// </summary>
    /// <typeparam name="TParam">The type of the param.</typeparam>
    /// <typeparam name="TResult">The type of the result.</typeparam>
    public class QuerySubscription<TParam, TResult>
    {
        #region Fields

        private Func<TParam, TResult> _function;

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="QuerySubscription&lt;TParam, TResult&gt;"/> class.
        /// </summary>
        /// <param name="queryName">Name of the query.</param>
        /// <param name="function">The function.</param>
        public QuerySubscription(string queryName, Func<TParam, TResult> function)
        {
            _function = function;
            Query<TParam, TResult> query = Container.Current.GetExportedValue<Query<TParam, TResult>>(Query.QualifiedName(queryName));
            query.Subscribe(_function);
        }

        #endregion Constructors
    }

    /// <summary>
    /// A subscription to a query.  
    /// </summary>
    public class QuerySubscription : QuerySubscription<object, object>
    {
        #region Constructors

        public QuerySubscription(string queryName, Func<object, object> function)
            : base(queryName, function)
        {
        }

        #endregion Constructors
    }
}